clear
set more off
capture log close

use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Mexico\Mexico 2006\base panel mexico 2006.dta", clear
gen respond1=1 if dia~=.
gen respond2=1 if dia_2~=.
gen respond3=1 if dia_3~=.
recode respond* (.=0)

gen pweight=1
replace pweight=160/(160+500) if estado==9
replace pweight=80/(80+140) if estado==7
replace pweight=100/(80+100) if estado==14
replace pweight=60/(80+60) if estado==20	
svyset [pweight=pweight], psu(municipi)

*note that those in the cross-sectional samples answered shorter questionnaires and were not reinterviewed
drop if tipomu_2==4
drop if tipomu_3==4
*-------------------------------------

recode p8 2=3 3=2 4/5=7 6=7 7/9=7 .=7, gen(vote_iv1)
label define vote_iv1 1 "Felipe Calderon" 3 "Roberto Madrazo" 2 "AMLO" 7 "Others or Abstain/NR"
label values vote_iv1 vote_iv1
replace vote_iv1=. if respond1~=1

recode p7_2 4=6 6=6 7/9=7 .=7, gen(vote2)
label define vote 1 "Felipe Calderon" 2 "Roberto Madrazo" 3 "AMLO" 6 "Others" 7 "Abstain/NR"
label values vote2 vote
replace vote2=. if respond2~=1

recode p6_3 4/6=6 7/9=7 .=7, gen(vote)
replace vote=. if respond3~=1
label values vote vote

		*Of voters with candidate preferences (excludes DK from denominator, and also none/abstention and nr)
			*in both waves (1 and 2; 2 and 3)
gen change12=0 if respond1==1 & respond2==1 & p8~=4 & p8~=6 & p8~=7 & p8~=8 & p8~=. & p7_2~=7 & p7_2~=9 & p7_2~=4 & p7_2~=6 & p7_2~=8 & p7_2~=.
replace change12=1 if p8~=p7_2 & p8<6 & p7_2<7 & change12==0
bysort change12: tab p8  p7_2, m
svy: tab change12


gen change23=0 if respond2==1 & respond3==1 & p7_2~=7 & p7_2~=9 & p7_2~=. & p6_3~=7 & p6_3~=9 & p7_2~=8 & p6_3~=8 & p6_3~=.
replace change23=1 if p7_2~=p6_3 & p7_2<7 & p6_3<7 & change23==0
bysort change23: tab p7_2 p6_3, m
svy: tab change23

gen change13=0 if respond1==1 & respond3==1 & p8~=4 & p8~=6 & p8~=7 & p8~=8 & p8~=.  & p6_3~=7 & p6_3~=9 & p6_3~=8 & p6_3~=.
replace change13=1 if p8~=p6_3 & p8<6 & p6_3<7 & change13==0
bysort change13: tab p8 p6_3, m
svy: tab change13

			*in all three waves (1-3)
gen changedatall=change23+change12
recode changedatall 2=1
svy: tab changedatall


*DEFINE INDEPENDENT VARIABLES
recode p22 2=.5 1=1 else=0, gen(panista)
recode p22 3=1 4=.5 else=0, gen(priista)
recode p22 5=1 6=.5 else=0, gen(prdista)
recode p22 7/9=1 else=0, gen(party_base)

recode p26 1=5 2=4 3=3 4=2 5=1 6=., gen(p26foxapproval)
gen p21felipe = (p21f)/10 
gen p21amlo = (p21h)/10 
gen p21madrazo = (p21i)/10 

gen religion = p58 
recode religion (1=4) (2=3) (3=2) (4=1) (5=0) (6=.) 
gen educ = p59 
recode educ (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=.) 
recode p64a-p64h (1=1) (2=0) (else=.)
irt 2pl p64a-p64h
predict wealth1, ebmeans latent
recode a 2=1 1=0, gen(woman)
rename d age
recode p2_2 1=4 2=3 3=2 4=1 else=.,gen(interest2)
recode p3_2 1=5 2=4 3=3 4=2 5=1 else=., gen(converse2)
recode p47_3 1=5 2=4 3=3 4=2 5=1 else=., gen(converse3)
recode p2_3 1=4 2=3 3=2 4=1 else=.,gen(interest3)
recode p70_2 11=., gen(income2)
recode p4_2 1=4 2=3 3=2 4=1 else=., gen(interestcamp2)
recode p3_3 1=4 2=3 3=2 4=1 else=., gen(interestcamp3)


/* QUESTION #39/40 */ 
gen capital = 4 if p39==1 & p40==1 
replace capital = 3 if (p39==1 & p40==2) | (p39==1 & p40==3) 
replace capital = 2 if p39==3 
replace capital = 1 if (p39==2 & p40==2) | (p39==2 & p40==3) 
replace capital = 0 if p39==2 & p40==1  

/* QUESTION #41/42 */ 
gen abortion = 0 if p41==1 & p42==1 
replace abortion = 1 if (p41==1 & p42==2) | (p41==1 & p42==3) 
replace abortion = 2 if p41==3 
replace abortion = 3 if (p41==2 & p42==2) | (p41==2 & p42==3) 
replace abortion = 4 if p41==2 & p42==1  

/* QUESTION #43/44 */ 
gen privatize = 4 if p43==1 & p44==1 
replace privatize = 3 if (p43==1 & p44==2) | (p43==1 & p44==3) 
replace privatize = 2 if p43==3 
replace privatize = 1 if (p43==2 & p44==2) | (p43==2 & p44==3) 
replace privatize = 0 if p43==2 & p44==1  

gen privatize2 = 4 if p53_2==1 & p54_2==1 
replace privatize2 = 3 if (p53_2==1 & p54_2==2) | (p53_2==1 & p54_2==3) 
replace privatize2 = 2 if p53_2==3 
replace privatize2 = 1 if (p53_2==2 & p54_2==2) | (p53_2==2 & p54_2==3) 
replace privatize2 = 0 if p53_2==2 & p54_2==1  

gen privatize3 = 4 if p37_3==1 & p38_3==1 
replace privatize3 = 3 if (p37_3==1 & p38_3==2) | (p37_3==1 & p38_3==3) 
replace privatize3 = 2 if p37_3==3 
replace privatize3 = 1 if (p37_3==2 & p38_3==2) | (p37_3==2 & p38_3==3) 
replace privatize3 = 0 if p37_3==2 & p38_3==1  

/* QUESTION #46/47 */ 
gen usatrade = 4 if p46==1 & p47==1 
replace usatrade = 3 if (p46==1 & p47==2) | (p46==1 & p47==3) 
replace usatrade = 2 if p46==3 | p46==4 
replace usatrade = 1 if (p46==2 & p47==2) | (p46==2 & p47==3) 
replace usatrade = 0 if p46==2 & p47==1 

gen usatrade2 = 4 if p56_2==1 
replace usatrade2 = 2 if p56_2==3 | p56_2==4 
replace usatrade2 = 0 if p56_2==2 

gen usatrade3 = 4 if p40_3==1 
replace usatrade3 = 3 if p40_3==2
replace usatrade3 = 2 if p40_3==5/8
replace usatrade3 = 1 if p40_3==3 
replace usatrade3 = 0 if p40_3==4 

recode tipo 1=3 2=1 3=2, gen(urban)

char p8[omit] 8 
xi i.p8
renpfix _Ip8_ lagvote
char vote[omit] 5
xi i.vote
renpfix _I

char p7_2[omit] 8 
xi i.p7_2
renpfix _Ip7_2_ lagvotet

char vote_iv1[omit] 1
xi i.vote_iv1
renpfix _I


*DISCUSSANT IVS
		*Count discussants
egen disctotal2=rsum( p66a_2 p66b_2 p66c_2)
replace disctotal2=. if respond2~=1
replace disctotal2=. if tipomu_2==4

egen disctotal3=rsum(p48a_3 p48b_3 p48c_3)
replace disctotal3=. if respond3~=1
replace disctotal3=. if tipomu_3==4

recode p3 p3_2 p47_3 (1=4) (2=3) (3=2) (4=1) (5=0) (6=.)
label define freq 0 Never 1 Rarely 2 "Few times per month" 3 "Few times per week" 4 "Daily"
label values p3 p3_2 p47_3 freq

*recode p3 p3_2 p47_3 (0=0) (1=1) (2=4) (3=15) (4=30)
rename p3 talk1
rename p3_2 talk2
rename p47_3 talk3
egen talkmean=rowmean(talk2 talk3)
egen disctotal=rowmean(disctotal2 disctotal3)

irt grm disctotal2 disctotal3 talk2 talk3
predict discuss, latent ebmeans
replace discuss=. if respond2==0 & respond3==0

gen news11=0 if p14a==1
replace news11=4 if p15a==1
replace news11=3 if p15a==2
replace news11=2 if p15a==3
replace news11=1 if p15a==4

gen news21=0 if p14b==1
replace news21=4 if p15b==1
replace news21=3 if p15b==2
replace news21=2 if p15b==3
replace news21=1 if p15b==4

egen news1=rowmax(news11 news21)

gen news12=0 if p15a_2==1
replace news12=4 if p16a_2==1
replace news12=3 if p16a_2==2
replace news12=2 if p16a_2==3
replace news12=1 if p16a_2==4

gen news22=0 if p15b_2==1
replace news22=4 if p16b_2==1
replace news22=3 if p16b_2==2
replace news22=2 if p16b_2==3
replace news22=1 if p16b_2==4

egen news2=rowmax(news12 news22)

gen news13=0 if p12a_3==1
replace news13=4 if p13a_3==1
replace news13=3 if p13a_3==2
replace news13=2 if p13a_3==3
replace news13=1 if p13a_3==4

gen news23=0 if p12b_3==1
replace news23=4 if p13b_3==1
replace news23=3 if p13b_3==2
replace news23=2 if p13b_3==3
replace news23=1 if p13b_3==4

egen news3=rowmax(news13 news23)

irt grm news12 news22 news13 news23
predict news, latent ebmeans
replace news=. if respond2==0 & respond3==0

*Party Contact
recode p41_2 (2=0) (1=1) (else=.), gen(partycontact2)
replace partycontact2=. if respond2==0

recode p28_3 (2=0) (1=1) (else=.), gen(partycontact3)
replace partycontact3=. if respond3==0

egen partycontact=rowmean(partycontact2 partycontact3)

*Clientelism
*Pre-Election (May) Wave
gen client_2=p42_2
replace client_2=. if client_2==3
recode client_2 (1=1) (2=0)
replace client_2=. if respond2==0

*Post-Election (July) Wave
gen client_3=p29_3
replace client_3=. if client_3==3 
recode client_3 (1=1) (2=0)
replace client_3=. if respond3==0

egen client=rowmean(client_2 client_3)

recode b 4=. 5=., gen(ethnic)

		*AWARENESS
recode p32a 4=1 1=0 2=0 3=0 5=0 6=0 7=0 8=0 9=0, gen(correct1)
recode p32b 2=1 1=0 3=0 4=0 5=0 6=0 7=0 8=0 9=1, gen(correct2)
recode p32c 1=1 2=0 3=0 4=0 5=0 6=0 7=0 8=1 9=0, gen(correct3)
recode p32d 3=1 1=0 2=0 4=0 5=0 6=0 7=1 8=0 9=0, gen(correct4)
recode p71 1=1 2=0 3=0 4=0, gen(correct5)
recode p72a-p72c p65a_3 p65b_3 p65c_3 (2=0)
gen correct6=p72a
gen correct7=p72b
gen correct8=p72c
gen correct12=p65a_3
gen correct13=p65b_3
gen correct14=p65c_3
egen correct_poderes=anycount(correct6 correct7 correct8), values(1)
egen correct_poderes3=anycount(correct12 correct13 correct14), values(1)
recode p69a_2 1=1 2/6=0 .=., gen(correct9)
replace correct9=. if respond2~=1
recode p69b_2 2=1 1=0 3/6=0 .=., gen(correct10)
replace correct10=. if respond2~=1
recode p69c_2 3=1 1/2=0 4/6=0 .=., gen(correct11)
replace correct11=. if respond2~=1
egen ft_know=rownonmiss(p21a p21b p21c p21d p21e p21f p21g p21h p21i p21j p21k p21l p21m)

svy: irt grm correct1 correct2 correct3 correct4 correct5 correct_poderes correct_poderes3 correct9 correct10 correct11 ft_know, difficult
predict irt_know, latent ebmeans
egen awarez=std(irt_know)

*PARTY CLIENTELISM
recode p30a_3-p30g_3 (1/6=1) (else=.)
gen pan_client3=1 if p30a_3==1 | p30e_3==1
gen pri_client3=1 if p30b_3==1 | p30f_3==1
gen prd_client3=1 if p30c_3==1 | p30g_3==1
gen other_client3=1 if p30h_3==1 | p30d_3==1
recode pan_client3 pri_client3 prd_client3 other_client3 (.=0)

recode p42a_2-p42g_2 (1/6=1) (else=.)
gen pan_client2=1 if p42a_2==1 | p42e_2==1
gen pri_client2=1 if p42b_2==1 | p42f_2==1
gen prd_client2=1 if p42c_2==1 | p42g_2==1
gen other_client2=1 if p42h_2==1 | p42d_2==1
recode pan_client2 pri_client2 prd_client2 other_client2 (.=0)

*PARTY CONTACTING 
gen pan_contact3=1 if p28a_3==1 | p28e_3==1
gen pri_contact3=1 if p28b_3==1 | p28f_3==1
gen prd_contact3=1 if p28c_3==1 | p28g_3==1
gen other_contact3=1 if p28d_3==1 | p28h_3==1
recode pan_contact3 pri_contact3 prd_contact3 other_contact3 (.=0)

gen pan_contact2=1 if p41a_2==1 | p41e_2==1
gen pri_contact2=1 if p41b_2==1 | p41f_2==1
gen prd_contact2=1 if p41c_2==1 | p41g_2==1
gen other_contact2=1 if p41h_2==1 | p41h_2==1
recode pan_contact2 pri_contact2 prd_contact2 other_contact2 (.=0)

gen pan_contact=1 if pan_client3==1 | pan_contact3==1 | pan_client2==1 | pan_contact2==1
gen pri_contact=1 if pri_client3==1 | pri_contact3==1 | pri_client2==1 | pri_contact2==1
gen prd_contact=1 if prd_client3==1 | prd_contact3==1 | prd_client2==1 | prd_contact2==1
recode pan_contact pri_contact prd_contact (.=0)

*MEDIA
label define tv 0 "none" 1 "televisa" 2 "azteca" 3 "other"

recode p13a_3 1=4 2=3 3=2 4=1, gen(tv_program1_freq)
recode p13b_3 1=4 2=3 3=2 4=1, gen(tv_program2_freq)
 
recode p12a_3 1=0 2=3 3=2 4=3 5=3 6=3 7=1 8=1 9/13=2 14=3 15=1 ///
16=1 17=1 18/19=3 20=1 21=2 22=3 23=0 24=3 25=3 26=1 27=1 28=3 29=3 30=3 31=3 32=3 .=., gen(tv_program1)
label values tv_program1 tv 
 
recode p12b_3 1=0 2=3 3=2 4=3 5=3 6=3 7=1 8=1 9/13=2 14=3 15=1 ///
16=1 17=1 18/19=3 20=1 21=2 22=3 23=0 24=3 25=3 26=1 27=1 28=3 29=3 30=3 31=3 32=3 .=., gen(tv_program2)
label values tv_program2 tv 

gen televisa_expose=0 if respond3==1
replace televisa_expose=tv_program1_freq if tv_program1==1 & tv_program2~=1
replace televisa_expose=tv_program2_freq if tv_program2==1 & tv_program1~=1
replace televisa_expose=tv_program1_freq + tv_program2_freq  if tv_program1==1 & tv_program2==1

gen azteca_expose=0 if respond3==1
replace azteca_expose=tv_program1_freq if tv_program1==2 & tv_program2~=2
replace azteca_expose=tv_program2_freq if tv_program2==2 & tv_program1~=2
replace azteca_expose=tv_program1_freq + tv_program2_freq  if tv_program1==2 & tv_program2==2

gen other_expose=0 if respond3==1
replace other_expose=tv_program1_freq if tv_program1==3 & tv_program2~=3
replace other_expose=tv_program2_freq if tv_program2==3 & tv_program1~=3
replace other_expose=tv_program1_freq + tv_program2_freq  if tv_program1==3 & tv_program2==3

***WAVE 2: DEFINING DISAGREEMENT AND HETEROGENEITY
egen numdisc2=rsum( p66a_2 p66b_2 p66c_2)
replace numdisc2=. if respond2~=1
replace p68a_2=. if p66a_2~=1
replace p68b_2=. if p66b_2~=1
replace p68c_2=. if p66c_2~=1

recode p68a_2-p68c_2 (1=1) (6=.) (7=.) (else=0), gen(felipe21 felipe22 felipe23)
recode p68a_2-p68c_2 (2=1) (6=.) (7=.) (else=0), gen(madrazo21 madrazo22 madrazo23)
recode p68a_2-p68c_2 (3=1) (6=.) (7=.) (else=0), gen(amlo21 amlo22 amlo23)
recode p68a_2-p68c_2 (4=1) (6=.) (7=.) (else=0), gen(campa21 campa22 campa23)
recode p68a_2-p68c_2 (5=1) (6=.) (7=.) (else=0), gen(mercado21 mercado22 mercado23)
recode p68a_2-p68c_2 (6=1) (7=1) (else=0), gen(noop21 noop22 noop23)
egen totopinionateddisc2=rsum(felipe21 felipe22 felipe23 madrazo21 madrazo22 madrazo23 amlo21 amlo22 amlo23 campa21 campa22 campa23 mercado21 mercado22 mercado23)
replace totopinionateddisc2=. if respond2~=1

gen agree21=0
replace agree21=1 if p68a_2==p7_2
replace agree21=. if p7_2>6
replace agree21=. if p68a_2>5
gen agree22=0
replace agree22=1 if p68b_2==p7_2
replace agree22=. if p7_2>6
replace agree22=. if p68b_2>5
gen agree23=0
replace agree23=1 if p68c_2==p7_2
replace agree23=. if p7_2>6
replace agree23=. if p68c_2>5
replace agree21=. if respond2~=1
replace agree22=. if respond2~=1
replace agree23=. if respond2~=1
egen numagree2=rsum(agree21-agree23)
replace numagree2=. if respond2==0
replace numagree2=. if p7_2>6

	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong2=1-numagree2/totopinionateddisc2
replace disagreementstrong2=0 if totopinionateddisc2==0
replace disagreementstrong2=. if p7_2>5

		*HETEROGENEITY 
recode agree21 0=1 1=0, gen(disagree21)
recode agree22 0=1 1=0, gen(disagree22)
recode agree23 0=1 1=0, gen(disagree23)
egen numdisagree2=rowtotal(disagree21 disagree22 disagree23)
forvalues i=1(1)3 {
replace felipe2`i'=. if agree2`i'==1
replace madrazo2`i'=. if agree2`i'==1
replace amlo2`i'=. if agree2`i'==1
replace campa2`i'=. if agree2`i'==1
replace mercado2`i'=. if agree2`i'==1
}

egen felipesum_numerator2=rsum(felipe21 felipe22 felipe23)
egen madrazosum_numerator2=rsum(madrazo21 madrazo22 madrazo23)
egen amlosum_numerator2=rsum(amlo21 amlo22 amlo23)
egen campasum_numerator2=rsum(campa21 campa22 campa23)
egen mercadosum_numerator2=rsum(mercado21 mercado22 mercado23)
gen felipediscprop2=felipesum_numerator2 / numdisagree2
gen madrazodiscprop2= madrazosum_numerator2 /numdisagree2
gen amlodiscprop2=amlosum_numerator2 /numdisagree2
gen campadiscprop2= campasum_numerator2/numdisagree2
gen mercadodiscprop2=mercadosum_numerator2/numdisagree2
gen heterogeneitystrong2 = 1-(felipediscprop2^2+madrazodiscprop2^2+amlodiscprop2^2+campadiscprop2^2+mercadodiscprop2^2)
recode heterogeneitystrong2 .=0
replace heterogeneitystrong2 =. if numdisagree2<2 & numdisagree2~=. & disagreementstrong2==. 
replace heterogeneitystrong2 =. if respond2==0
replace heterogeneitystrong2 =. if p7_2>5
drop disagree21 disagree22 disagree23  numdisagree2

		*SINGLETON
recode totopinionateddisc2 0=1 .=. else=0, gen(zerodiscstrong2)
replace zerodiscstrong2=. if respond2==0

	*WEAK
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak2=1-numagree2/numdisc2
replace disagreementweak2=0 if numdisc2==0
replace disagreementweak2=. if p7_2>5

		*HETEROGENEITY 
replace agree21=0 if p68a_2>5 & p66a_2==1 & p7_2<6
replace agree22=0 if p68b_2>5 & p66b_2==1 & p7_2<6
replace agree23=0 if p68c_2>5 & p66c_2==1 & p7_2<6

recode agree21 0=1 1=0, gen(disagree21)
recode agree22 0=1 1=0, gen(disagree22)
recode agree23 0=1 1=0, gen(disagree23)

egen numdisagree2=rowtotal(disagree21 disagree22 disagree23)
forvalues i=1(1)3 {
replace noop2`i'=. if disagree2`i'==0  
}

egen noopsum_numerator2=rsum(noop21 noop22 noop23)
gen felipediscprop2weak=felipesum_numerator2 / numdisagree2
gen madrazodiscprop2weak= madrazosum_numerator2 /numdisagree2
gen amlodiscprop2weak=amlosum_numerator2 /numdisagree2
gen campadiscprop2weak= campasum_numerator2/numdisagree2
gen mercadodiscprop2weak=mercadosum_numerator2/numdisagree2
gen noopdiscprop2weak=noopsum_numerator2/numdisagree2
gen heterogeneityweak2 = 1-(felipediscprop2weak^2+madrazodiscprop2weak^2+amlodiscprop2weak^2+campadiscprop2weak^2+mercadodiscprop2weak^2+noopdiscprop2weak^2)
recode heterogeneityweak2 .=0
replace heterogeneityweak2 =. if numdisagree2<2 & numdisagree2~=. & disagreementweak2==. 
replace heterogeneityweak2=. if respond2==0
replace heterogeneityweak2=. if p7_2>5
recode heterogeneityweak2 1=. 

		*SINGLETONS
recode disctotal2 0=1 .=. else=0, gen(zerodiscweak2)
replace zerodiscweak2=. if respond2==0


*GET CORRECT VOTING SCORE
	*Valence
recode p54 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(socio1)
recode p55 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(pocket1)
recode p61_3 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(socio3)
recode p62_3 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(pocket3)

recode p26 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(fox_approve1)
recode p24_2 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(fox_approve2)
recode p20_3 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(fox_approve3)
	*Positional
recode p73 7=1 6=.666 5=.3333 4=0 3=-.3333 2=-.6666 1=-1 8=. 9=., gen(ideo1) 
recode p62_2 7=1 6=.666 5=.3333 4=0 3=-.3333 2=-.6666 1=-1 8=. 9=., gen(ideo2) 
recode p46_3 7=1 6=.666 5=.3333 4=0 3=-.3333 2=-.6666 1=-1 8=. 9=., gen(ideo3) 

recode privatize 4=1 3=.3333 2=. 1=-.3333 0=-1, gen(priv1)
recode privatize2 4=1 3=.3333 2=. 1=-.3333 0=-1, gen(priv2)
recode privatize3 4=1 3=.3333 2=. 1=-.3333 0=-1, gen(priv3)

recode usatrade 4=1 3=.5 2=0 1=-.5 0=-1, gen(trade1)
replace usatrade=. if p46==4

recode usatrade2 4=.75 2=0 1=-.75 0=-1, gen(trade2)
replace usatrade2=. if p56_2==4

recode usatrade3 4=1 3=.5 2=0 1=-.5 0=-1, gen(trade3)
replace usatrade3=. if p40_3==8

	*Cands on positional
recode p39a_3 p39b_3 p39c_3 (1=1) (3=0) (2=-1)
summ p39a_3 p39b_3 p39c_3 if awarez>.5

recode p41a_3 p41b_3 p41c_3 (1=1) (2=-1) (3=0) (4=.)
summ p41a_3 p41b_3 p41c_3  if awarez>.5

*from W-B, for overall ideo
display -1.1052+.1052*16.45 /*FC*/
display -1.1052+.1052*11.55 /*PRI*/
display -1.1052+.1052*4.55 /*PRD*/

*from W-B, for relig
display -1.1052+.1052*12.33 /*FC*/
display -1.1052+.1052*4.57 /*PRI*/
display -1.1052+.1052*2.66 /*PRD*/

	*Groups
recode p58 1=1 2=.5 3=0 4=-.5 5=-1 6=., gen(relig1)
recode urban 3=1 2=0 1=-1, gen(urban1)

recode p44b_2 1=1 2=.333 3=-.3333 4=-1 5=., gen(madrazo_win)

capture program drop cv
program define cv
while "`1'" ~= "" {
gen felipe_`1'=-(abs(`1'-`2')-1)
gen madrazo_`1'=-(abs(`1'-`3')-1)
gen amlo_`1'=-(abs(`1'-`4')-1)
mac shift
mac shift
mac shift
mac shift
}
exit
end

cv socio1 1 -1 -1 pocket1 1 -1 -1 
cv socio3 1 -1 -1 pocket3 1 -1 -1 
cv fox_approve1 1 -1 -1 fox_approve2 1 -1 -1  fox_approve3 1 -1 -1 
cv ideo1 .625 .10986 -.62654 ideo2 .625 .10986 -.62654 ideo3 .625 .10986 -.62654 
cv priv1 .6336 .2368 -.4176 priv2 .6336 .2368 -.4176 priv3 .6336 .2368 -.4176 trade1 .74 .5078 -.17147 trade2 .74 .5078 -.17147 trade3 .74 .5078 -.17147
cv relig1 .19 -.624 -.825 urban1 1 -1 0 madrazo_win -1 1 -1

*egen felipe1_av=rowmean(felipe_socio1 felipe_fox_approve1 felipe_ideo1 felipe_priv1 felipe_trade1 felipe_relig1  )
*egen madrazo1_av=rowmean(madrazo_socio1 madrazo_fox_approve1 madrazo_ideo1 madrazo_priv1 madrazo_trade1 madrazo_relig1 )
*egen amlo1_av=rowmean(amlo_socio1 amlo_fox_approve1 amlo_ideo1 amlo_priv1 amlo_trade1 amlo_relig1 amlo_urban1 amlo_relig1 )

*egen felipe3_av=rowmean(felipe_socio3 felipe_fox_approve3 felipe_ideo3 felipe_priv3 felipe_trade3 felipe_relig1 )
*egen madrazo3_av=rowmean(madrazo_socio3 madrazo_fox_approve3 madrazo_ideo3 madrazo_priv3 madrazo_trade3 madrazo_relig1 )
*egen amlo3_av=rowmean(amlo_socio3 amlo_fox_approve3 amlo_ideo3 amlo_priv3 amlo_trade3 amlo_relig1 )

egen felipe1_av=rowtotal(felipe_socio1 felipe_fox_approve1 felipe_ideo1 felipe_priv1 felipe_trade1 felipe_relig1 felipe_madrazo_win)
egen madrazo1_av=rowtotal(madrazo_socio1 madrazo_fox_approve1 madrazo_ideo1 madrazo_priv1 madrazo_trade1 madrazo_relig1 madrazo_madrazo_win)
egen amlo1_av=rowtotal(amlo_socio1 amlo_fox_approve1 amlo_ideo1 amlo_priv1 amlo_trade1 amlo_relig1 amlo_urban1 amlo_relig1 amlo_madrazo_win)

egen felipe3_av=rowtotal(felipe_socio3 felipe_fox_approve3 felipe_ideo3 felipe_priv3 felipe_trade3 felipe_relig1 felipe_madrazo_win)
egen madrazo3_av=rowtotal(madrazo_socio3 madrazo_fox_approve3 madrazo_ideo3 madrazo_priv3 madrazo_trade3 madrazo_relig1 madrazo_madrazo_win)
egen amlo3_av=rowtotal(amlo_socio3 amlo_fox_approve3 amlo_ideo3 amlo_priv3 amlo_trade3 amlo_relig1 amlo_madrazo_win)

gen correct_vote1=1 if felipe1_av>madrazo1_av & felipe1_av>amlo1_av
replace correct_vote1=2 if madrazo1_av>felipe1_av & madrazo1_av>amlo1_av
replace correct_vote1=3 if amlo1_av>felipe1_av & amlo1_av>madrazo1_av

gen correct_vote3=1 if felipe3_av>madrazo3_av & felipe3_av>amlo3_av
replace correct_vote3=2 if madrazo3_av>felipe3_av & madrazo3_av>amlo3_av
replace correct_vote3=3 if amlo3_av>felipe3_av & amlo3_av>madrazo3_av

gen voted_correctly1=1 if correct_vote1==p8 & p8<4
replace voted_correctly1=0 if correct_vote1~=p8 & p8<4

gen voted_correctly1to2=1 if correct_vote1==p7_2 & p7_2<4
replace voted_correctly1to2=0 if correct_vote1~=p7_2 & p7_2<4

gen voted_correctly1to3=1 if correct_vote1==p6_3 & p6_3<4
replace voted_correctly1to3=0 if correct_vote1~=p6_3 & p6_3<4

gen voted_correctly3=1 if correct_vote3==p6_3 & p6_3<4
replace voted_correctly3=0 if correct_vote3~=p6_3 & p6_3<4

	*Static stuff
*Figure 8.4, Table C.1
eststo clear
eststo: svy: logit voted_correctly1to3 awarez i.p6_3
margin, at((p5) awarez) at((p95) awarez) contrast(atcontrast(r)) atmeans

eststo: svy: logit voted_correctly1to3 talkmean news partycontact i.p6_3
margin, at((p5) talkmean) at((p95) talkmean) contrast(atcontrast(r)) atmeans
margin, at((p5) news) at((p95) news) contrast(atcontrast(r)) atmeans
margin, at((p5) partycontact) at((p95) partycontact) contrast(atcontrast(r)) atmeans
*margin, at((p5) client) at((p95) client) contrast(atcontrast(r)) atmeans

svy: logit voted_correctly1to3 discuss news partycontact i.p6_3
svy: logit voted_correctly1to3 disctotal news partycontact i.p6_3

eststo: svy: logit voted_correctly1to3 wealth1 educ woman ethnic i.p6_3
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
xyz
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Output\"
esttab est1 est2 est3 using mexico06cv.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(CV Mexico 06)

	*Change stuff
svy: logit voted_correctly1to3 change13 voted_correctly1 i.p6_3
*Table 8.1
svy: logit voted_correctly1to3 change23 voted_correctly1to2 i.p6_3 if voted_correctly1~=. & voted_correctly1to3~=. & voted_correctly1to2~=.
xyz
svy: logit voted_correctly1to3 c.discuss##i.change23 voted_correctly1 i.p6_3
svy: logit voted_correctly1to3 discuss voted_correctly1to2 i.p6_3

svy: tab voted_correctly1 
svy: tab voted_correctly1to2 
svy: tab voted_correctly1to3 

svy: tab voted_correctly1 if voted_correctly1to2~=. & voted_correctly1to3~=.
svy: prop voted_correctly1to2 if voted_correctly1~=. & voted_correctly1to3~=.
svy: prop voted_correctly1to3 if voted_correctly1~=. & voted_correctly1to2~=.

	*Disagreement stuff
svy: logit voted_correctly1to3 disagreementweak2 zerodiscweak2 voted_correctly1to2 i.p6_3
svy: logit voted_correctly1to3 disagreementweak2 zerodiscweak2 news partycontact  i.p6_3
svy: logit voted_correctly1to3 disagreementstrong2 zerodiscstrong2 news partycontact  i.p6_3

xyz



/*
*WAVE 2
	*strong definition
recode p68a_2-p68c_2 (1=1) (else=0), gen(felipe21 felipe22 felipe23)
recode p68a_2-p68c_2 (2=1) (else=0), gen(madrazo21 madrazo22 madrazo23)
recode p68a_2-p68c_2 (3=1) (else=0), gen(amlo21 amlo22 amlo23)
recode p68a_2-p68c_2 (4=1) (else=0), gen(campa21 campa22 campa23)
recode p68a_2-p68c_2 (5=1) (else=0), gen(mercado21 mercado22 mercado23)
egen felipedisc2=rsum(felipe2*)
egen madrazodisc2=rsum(madrazo2*)
egen amlodisc2=rsum(amlo2*)
egen campadisc2=rsum(campa2*)
egen mercadodisc2=rsum(mercado2*)
egen totopinionateddisc2=rsum(felipe21 felipe22 felipe23 madrazo21 madrazo22 madrazo23 amlo21 amlo22 amlo23 campa21 campa22 campa23 mercado21 mercado22 mercado23)
replace totopinionateddisc2=. if respond2~=1
gen felipediscper2=felipedisc2/totopinionateddisc2
gen madrazodiscper2=madrazodisc2/totopinionateddisc2
gen amlodiscper2=amlodisc2/totopinionateddisc2
gen otherdiscper2=(campadisc2+mercadodisc2)/totopinionateddisc2
recode *discper2 (.=0)
recode totopinionateddisc2 0=1 1/3=0 .=., gen(zerodiscstrong2)

	*extra stuff for compositional analysis
replace felipediscper2 =. if respond2~=1
replace madrazodiscper2 =. if respond2~=1
replace amlodiscper2 =. if respond2~=1
replace zerodiscstrong2=. if respond2~=1 
replace otherdiscper2=. if respond2~=1

	*weak definition
egen numdisc2=rsum( p66a_2 p66b_2 p66c_2)
replace numdisc2=. if respond2~=1
gen felipediscperweak2=felipedisc2/numdisc2
gen madrazodiscperweak2=madrazodisc2/numdisc2
gen amlodiscperweak2=amlodisc2/numdisc2
gen otherdiscperweak2=(campadisc2+mercadodisc2)/numdisc2
gen nonediscperweak2=1-(felipedisc2+madrazodisc2+amlodisc2+campadisc2+mercadodisc2)/numdisc2
replace felipediscperweak2=0 if numdisc2==0 & felipediscperweak2==.
replace madrazodiscperweak2=0 if numdisc2==0 & madrazodiscperweak2==.
replace amlodiscperweak2=0 if numdisc2==0 & amlodiscperweak2==.
replace otherdiscperweak2=0 if numdisc2==0 & otherdiscperweak2==.
replace nonediscperweak2=0 if numdisc2==0 & nonediscperweak2==.
tab felipediscperweak2 felipediscper2, m
recode numdisc2 0=1 1/3=0 .=., gen(zerodiscweak2)

/**WAVE 3
recode p50a_3-p50c_3 (1=1) (else=0), gen(felipe31 felipe32 felipe33)
recode p50a_3-p50c_3 (2=1) (else=0), gen(madrazo31 madrazo32 madrazo33)
recode p50a_3-p50c_3 (3=1) (else=0), gen(amlo31 amlo32 amlo33)
recode p50a_3-p50c_3 (4=1) (else=0), gen(campa31 campa32 campa33)
recode p50a_3-p50c_3 (5=1) (else=0), gen(mercado31 mercado32 mercado33)
egen felipedisc3=rsum(felipe3*)
egen madrazodisc3=rsum(madrazo3*)
egen amlodisc3=rsum(amlo3*)
egen campadisc3=rsum(amlo3*)
egen mercadodisc3=rsum(amlo3*)
egen totopinionateddisc3=rsum(felipe31 felipe32 felipe33 madrazo31 madrazo32 madrazo33 amlo31 amlo32 amlo33 campa31 campa32 campa33 mercado31 mercado32 mercado33)
replace totopinionateddisc3=. if respond3~=1
gen felipediscper3=felipedisc3/totopinionateddisc3
gen madrazodiscper3=madrazodisc3/totopinionateddisc3
gen amlodiscper3=amlodisc3/totopinionateddisc3
recode *discper3 (.=0)
recode totopinionateddisc3 0=1 1/3=0 .=., gen(zerodiscstrong3)

replace felipediscper3 =. if respond3~=1
replace madrazodiscper3 =. if respond3~=1
replace amlodiscper3 =. if respond3~=1
replace zerodiscstrong3=. if respond3~=1 
egen numdisc3=rsum(p48a_3 p48b_3 p48c_3)
replace numdisc3=. if respond3~=1
gen felipediscperweak3=felipedisc3/numdisc3
gen madrazodiscperweak3=madrazodisc3/numdisc3
gen amlodiscperweak3=amlodisc3/numdisc3
replace felipediscperweak3=0 if numdisc3==0 & felipediscperweak3==.
replace madrazodiscperweak3=0 if numdisc3==0 & madrazodiscperweak3==.
replace amlodiscperweak3=0 if numdisc3==0 & amlodiscperweak3==.
tab felipediscperweak3 felipediscper3, m
recode numdisc3 0=1 1/3=0 .=., gen(zerodiscweak3)
*/
